<template>
	<view class="examin_detail">
		<uni-forms :model="formData" label-position="left" ref="form" :rules="rules">
			<uni-forms-item label="名字" name="name">
				<uni-easyinput :disabled="true" type="text" v-model="formData.name" placeholder="名字" />
			</uni-forms-item>
			<!-- <uni-forms-item label="地址" name="location">
				<uni-easyinput :disabled="isLoading" type="text" v-model="formData.oldPassword" placeholder="请选择位置" />
			</uni-forms-item> -->
			<uni-forms-item label="项目" name="project_id">
				<uni-data-picker class="w100" placeholder="请选择项目" popup-title="请选择项目" :localdata="dataTree"
					v-model="formData.project_id" @change="onProjectchange"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item label="类型" name="type">
				<uni-data-picker class="w100" placeholder="请选择类型" popup-title="请选择类型" :localdata="typeTree"
					v-model="formData.type" @change="onTypeChange"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item label="工时" name="days">
				<uni-easyinput :disabled="Number(formData.square_meter.length)!==0 ?true : false" type="digit"
					v-model="formData.days" placeholder="请输入工时" />
			</uni-forms-item>
			<uni-forms-item label="平方米" name="square_meter">
				<uni-easyinput :disabled="Number(formData.days.length)!==0 ? true : false" type="digit"
					v-model="formData.square_meter" placeholder="请输入平方米" />
			</uni-forms-item>
			<uni-forms-item>
				<button type="primary" :disabled="isLoading" @click="submit" class="w100">
					提交
				</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentItem: null,
				index: null,
				isLoading: false,
				dataTree: [],
				typeTree: [],
				formData: {
					name: "",
					location: "",
					project_id: "",
					square_meter: '',
					user_id: '',
					days: '',
					type: ""
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入',
						}]
					},
					project_id: {
						rules: [{
							errorMessage: '请输入',
						}]
					},
					days: {
						rules: [{
							errorMessage: '请输入',
						}]
					},
					square_meter: {
						rules: [{
							errorMessage: '请输入',
						}]
					},
					type: {
						rules: [{
							errorMessage: '请选择',
						}]
					},

				}
			}
		},
		methods: {
			async getTypes(state) {
				const res = await this.$request.request({
					url: this.$api.getProject,
					method: "GET",
					isNotLoading: true,
				});
				this.dataTree = this.getValue(res);
			},
			getLocation() {
				return new Promise((resolve, reject) => {
					uni.chooseLocation({
						success: function(res) {
							const location = res.name + res.address
							if (location != '' && location.length > 0) {
								resolve(location)
							} else {
								resolve("")
							}
						},
						fail: function(e) {
							console.log('fail');
							resolve("")
							this.$errMessageToast(e | e.message | '请求地理位置出错')
						}
					});
				})
			},
			vaildType() {
				if (
					!this.formData.days &&
					!this.formData.square_meter
				) {
					throw new Error("请输入工时或者平方米任意之一");
				}

				return true;
			},
			async upload() {
				const location = await this.getLocation()
				if (location) {
					this.formData.location = location
				}
				this.isLoading = true;
				uni.showLoading({
					title: "正在上传...",
				});
				const data = {
					...this.formData
				}
				delete data.name
				if (data.square_meter === '') {
					delete data.square_meter
				}
				if (data.days === '') {
					delete data.days
				}
				try {
					const result = await this.$request.request({
						url: this.$api.auditAddWorker,
						data: data,
						header: this.$aplsjsonheader,
						isToast: true,
					});
					setTimeout(() => {
						let pages = getCurrentPages(); // 当前页面
						let beforePage = pages[pages.length - 2]; // 前一个页面
						uni.navigateBack({
							delta: 1,
							success: () => {
								beforePage && beforePage.$vm.reloadPage(this.index); // 执行前一个页面的刷新
							},
						})
					}, 1200)
				} catch (e) {
					console.log(e);
					//TODO handle the exception
				}
			},
			async submit() {
				this.$refs.form.validate().then(async (res) => {

					try {
						if (this.vaildType()) {
							this.upload()
						}
					} catch (e) {
						this.$errMessageToast(e.message || e);
						//TODO handle the exception
					}
				}).catch(err => {
					console.log('表单错误信息：', err);

				})

			},
			onProjectchange(e) {
				this.formData.project_id = e.detail.value[0].value;
			},
			onTypeChange(e) {
				this.formData.type = e.detail.value[0].value;
			},
			onchange(e) {
				this.formData.project_id = e.detail.value[0].value;
			},
			getValue(data) {
				const arr = [...data];
				const list = arr.map((i) => {
					return {
						text: i.name,
						value: i.id,
					};
				});
				return list;
			},
			getType(data) {
				const arr = [...data];
				const list = arr.map((i) => {
					return {
						text: i.name,
						value: i.name,
					};
				});
				return list;
			},
			async getProjects() {
				const res = await this.$request.request({
					url: this.$api.getProject,
					method: "GET",
					isNotLoading: true,
				});
				this.dataTree = this.getValue(res);
			},
			async getTypes() {
				const res = await this.$request.request({
					url: this.$api.getType,
					method: "GET",
					isNotLoading: true,
				});
				this.typeTree = this.getType(res);
			},
		},
		created() {
			this.$eventBus.$on("examin_detail_ready", (item, index) => {
				this.currentItem = item;
				this.formData.name = item.user_name
				this.formData.user_id = item.worker_id
				this.index = index
			});
			this.getProjects()
			this.getTypes()
		},
		mounted() {
			this.$eventBus.$emit('examine')
		}

	}
</script>

<style scoped lang="less">
	.examin_detail {
		background-color: #fdfdfd;
		height: auto;
		min-height: 100%;
		padding: 20rpx;
	}
</style>
